//
//  studentViewController.m
//  StuInfo
//
//  Created by Weasleyqi on 12-10-31.
//  Copyright (c) 2012年 weasleyqi CO., LTD. All rights reserved.
//

#import "studentViewController.h"

@interface studentViewController ()

@end

@implementation studentViewController
@synthesize num;
@synthesize classname;
@synthesize name;
@synthesize status;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
    // 一:创建数据库
    NSString *docsDir;
    NSArray *dirPaths;
    
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  //  NSLog(@"dirpaths = %@",dirPaths);
    docsDir = [dirPaths objectAtIndex:0];
  //  NSLog(@"docsDir = %@",docsDir);
    databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"info.db"]];
    NSLog(@"databasePath = %@",databasePath);
    NSFileManager *filemanager = [NSFileManager defaultManager];
    
    if ([filemanager fileExistsAtPath:databasePath] == NO) {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
            char *errmsg;
            const char *createsql = "CREATE TABLE IF NOT EXISTS INFO (id INTEGER PRIMARY KEY AUTOINCREMENT, NUM TEXT, CLASSNAME TEXT,NAME TEXT)";
            
            if (sqlite3_exec(dataBase, createsql, NULL, NULL, &errmsg)!=SQLITE_OK) {
                status.text = @"create table failed.";
            }
        }
        else {
            status.text = @"create/open failed.";
        }
    }
}

//保存信息
- (IBAction)saveinfo:(id)sender {
    
    sqlite3_stmt *statement;
    
    const char *dbpath = [databasePath UTF8String];
    
    if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
        if ([num.text isEqualToString:@""]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SORRY!" message:@"number cannot be nil!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else {
        
        NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO INFO (num,classname,name) VALUES(\"%@\",\"%@\",\"%@\")",num.text,classname.text,name.text];
        const char *insertsatement = [insertSql UTF8String];
        sqlite3_prepare_v2(dataBase, insertsatement, -1, &statement, NULL);
        if (sqlite3_step(statement)==SQLITE_DONE) {
            status.text = @"save to DB.";
            num.text = @"";
            classname.text = @"";
            name.text = @"";
        }
        else {
            status.text = @"save failed!";
        }
        sqlite3_finalize(statement);
        sqlite3_close(dataBase);
        }
    }
}

// 查询信息
- (IBAction)searchResult:(id)sender {
    const char *dbpath = [databasePath UTF8String];
    
    sqlite3_stmt *statement;
    
    if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
        NSString *querySQL = [NSString stringWithFormat:@"SELECT classname,name from info where num=\"%@\"",num.text];
        const char *querystatement = [querySQL UTF8String];
        if (sqlite3_prepare_v2(dataBase, querystatement, -1, &statement, NULL)==SQLITE_OK) {
            if (sqlite3_step(statement)==SQLITE_ROW) {
                NSString *classnameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
                classname.text = classnameField;
                NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
                name.text = nameField;
                
                status.text = @"find~~~";                
            }
            else {
                status.text = @"did not find you need.";
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(dataBase);
    }
}

// 清理信息
- (IBAction)clearTextField:(id)sender {
    num.text = @"";
    classname.text = @"";
    name.text = @"";
    status.text = @"";
}


//点击屏幕收起键盘
- (IBAction)backgroundTap:(id)sender {
    [num resignFirstResponder];
    [classname resignFirstResponder];
    [name resignFirstResponder];
}

- (void)viewDidUnload
{
    [self setNum:nil];
    [self setClassname:nil];
    [self setName:nil];
    [self setStatus:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
